define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'message/index',
                    add_url: 'message/add',
                    edit_url: 'message/edit',
                    del_url: '',
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'group.name', title: __('接收组'),formatter: Controller.formatter.groupname,align:'left',operate:false},
                        {field: 'type', title: __('发送内容'),operate:false},
                        {field: 'create_at', title: __('发送时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate',width:"125px",align:'right', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                    name: 'detail',
                                    text: '',
                                    title:'查看',
                                    icon: 'fa fa-eye',
                                    classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                    url: 'message/detail?ids={ids}'
                                }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // 选择类
            var appmsgType = 'text';
            $("body").on('click',"input[name='row[type]']",function(){
                if($(this).val() == appmsgType ){
                    if(appmsgType == 'mpnews'){
                        Fast.api.open($(this).data('href'),'选择图文')
                    }
                    return false;
                }
                appmsgType = $(this).val();
                $("._content").hide();
                $(".content-"+appmsgType).show();
                // 如果是图文
                if(appmsgType == 'mpnews'){
                    Fast.api.open($(this).data('href'),'选择图文')
                }
            })
            // 上传成功
            $("#faupload-image").data("upload-success", function(rest){
                $("#_media_id").val(rest.media_id)
            }); 
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        formatter:{
            groupname:function(value){
                return value == '' || value==null ? '全部用户' : value;
            }
        }
        
    };
    return Controller;
});
